import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_yres/model/user/jyz_order_list_model.dart';

class JYZOrderListPage extends StatefulWidget {
  String status;

  JYZOrderListPage({Key key, this.status}) : super(key: key);

  @override
  _JYZOrderListPageState createState() => _JYZOrderListPageState(status);
}

class _JYZOrderListPageState extends State<JYZOrderListPage> {
  String status;
  _JYZOrderListPageState(this.status);

  EasyRefreshController _controller = new EasyRefreshController();
  double _listHeight = ScreenUtil.getInstance().setHeight(200); //单个列表高度
  List<JYZOrderModelData> list = [];

  int page = 1;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: new EasyRefresh(
          controller: _controller,
          child: CustomScrollView(slivers: <Widget>[
            SliverFixedExtentList(
                delegate: SliverChildBuilderDelegate(_buildListItem,
                    childCount: list.length),
                itemExtent: _listHeight)
          ]),
          onRefresh: () async {
            page = 1;
          },
          onLoad: () async {
            page++;
          }),
    );
  }

  Widget _buildListItem(BuildContext context, int index) {
    return new GestureDetector(
        onTap: () {
          //处理点击事件
         JYZOrderModelData model =  list[index];
          // // var gasIds=model.gasid;
          //   Navigator.push(context, MaterialPageRoute(    //导航打开新视图
          //     builder: (context) => JYZDetailPage(model:model) //打开第二个页面，并传值，传递值的名称是model
          //   ));
        },
        child: Container(
            margin: EdgeInsets.fromLTRB(0, ScreenUtil.getInstance().setHeight(15), 0, 0),
            child: Column(
              children: <Widget>[
                Row(children: <Widget>[
                  // _leftImage(index),
                  Expanded(
                    child: _rightColumn(index),
                  )
                ], crossAxisAlignment: CrossAxisAlignment.start),
                Divider(
                  height: 1,
                )
              ],
            )));
  }
  Widget _pd(Widget w, {double l, double t, double r, double b}) {
    //边距
    return Padding(
        padding: EdgeInsets.fromLTRB(
            ScreenUtil.getInstance().setWidth(l) ?? 0,
            ScreenUtil.getInstance().setHeight(t) ?? 0,
            ScreenUtil.getInstance().setWidth(r) ?? 0,
            ScreenUtil.getInstance().setHeight(b) ?? 0),
        child: w);
  }

  Widget _leftImage(int index) {
    //左图
    var image = new Image(
        image: NetworkImage(list[index].gaslogosmall),
        width: ScreenUtil.getInstance().setHeight(150),
        height: ScreenUtil.getInstance().setHeight(150),
        fit: BoxFit.fill);
    return _pd(image, l: 20, t: 0, r: 5, b: 40);
  }

  Widget _rightColumn(int index) {
    //右内容
    var title = Text(
      list[index].gasName,
      style: TextStyle(color: Colors.black, fontSize: 16.0),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
    var info = RichText(
      text: TextSpan(style: TextStyle(fontSize: 13.0), children: [
        TextSpan(
            text: '加油信息:'+list[index].oilname.toString()+list[index].oilNo.toString(),
            style: TextStyle(color: Colors.grey)),

      ]),
    );

    var price = RichText(
      text: TextSpan(style: TextStyle(fontSize: 13.0), children: [
        TextSpan(
            text: '支付金额:'+list[index].amountPay.toString(),
            style: TextStyle(color: Colors.grey)),

      ]),
    );
     var time = RichText(
      text: TextSpan(style: TextStyle(fontSize: 13.0), children: [
        TextSpan(
            text: '支付时间:'+list[index].payTime.toString(),
            style: TextStyle(color: Colors.grey)),

      ]),
    );
    return new Column(
      children: <Widget>[
        _pd(title, l: 10, t: 0, r: 10, b: 8),
        _pd(info, l: 10, t: 0, r: 10, b: 8),
        _pd(price, l: 10, t: 0, r: 10, b: 8),
        _pd(time, l: 10, t: 0, r: 10, b: 8)
      ],
      crossAxisAlignment: CrossAxisAlignment.start,
    );
  }


  @override
  void initState() {
    _getLoginMsg();
    super.initState();
  }

//获取用户登录信息
  void _getLoginMsg() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    var phone = preferences.get("name");
    if (phone == null || phone == '') {
      Navigator.of(context).pushNamed("login");
    } else {
      getOrderList(phone);
    }
  }

  //  kGas_OrderURL
  getOrderList(phone) async {
    FormData data =
        new FormData.fromMap({'phone': phone, 'orderStatus': this.status});
    Response<String> response =
        await HttpUtil().post(Api.kGas_OrderURL, data: data);
    Map userMap = json.decode(response.data);
    JYZOrderModelEntity jyzEntity = new JYZOrderModelEntity.fromJson(userMap);
    print(userMap);
    if (jyzEntity.code == 0) {
      if (mounted) {
        setState(() {
          if (page == 1) {
            list.clear();
            list = jyzEntity.data;
          } else {
            list.addAll(jyzEntity.data);
          }
        });
      }
    }
  }
}
